package com.oocl.euc.core.maintenance.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.euc.core.builder.TeamBuilder;
import com.oocl.euc.core.builder.TeamEndorserBuilder;
import com.oocl.euc.core.builder.UsersBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.Team;
import com.oocl.euc.core.maintenance.model.TeamEndorser;
import com.oocl.euc.core.maintenance.model.Users;
import com.oocl.euc.core.maintenance.repository.TeamEndorserRepository;
import com.oocl.euc.core.maintenance.repository.TeamRepository;
import com.oocl.euc.core.maintenance.repository.UsersRepository;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.*;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class TeamEndorserControllerIT extends ITTestBase {

    @Autowired
    TeamBuilder teamBuilder;

    @Autowired
    TeamEndorserBuilder teamEndorserBuilder;

    @Autowired
    UsersBuilder usersBuilder;

    @Autowired
    TeamRepository teamRepository;

    @Autowired
    UsersRepository usersRepository;

    @Autowired
    TeamEndorserRepository teamEndorserRepository;

    private List<String> usersIdList = new ArrayList<>();

    @After
    public void reset() {
        teamEndorserRepository.deleteAll();
        teamRepository.deleteAll();
        for(String id : usersIdList) {
            usersRepository.deleteById(id);
        }
        usersIdList.clear();
    }

    @Test
    public void should_return_team_endorser_vo_when_given_team_id() throws Exception {
        Team team = teamBuilder.defaultBuilder()
                .withTeamName("TEST TEAM")
                .withTeamDesc("TEST DESC")
                .saveToDB();
        Users user = usersBuilder.defaultBuilder()
                .withStaffId("WUNE")
                .withDomainId("WUNE")
                .withDisplayName("WUNE")
                .saveToDB();
        usersIdList.add(user.getStaffId());
        TeamEndorser teamEndorser = teamEndorserBuilder.defaultBuilder()
                .withEndorser(user.getDomainId())
                .withDisplayName(user.getDisplayName())
                .withUser(user)
                .saveToDB();
        team.setTeamEndorsers(Arrays.asList(teamEndorser));
        teamRepository.save(team);

        mockMvc.perform(get("/rest/teamEndorser/displayName")
                .param("teamId", String.valueOf(team.getId())))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", Matchers.hasSize(1)))
                .andExpect(jsonPath("$[0].domainId").value("WUNE"))
                .andExpect(jsonPath("$[0].displayName").value("WUNE"));

    }

    @Test
    public void should_return_new_team_endorser_when_given_team_name_and_endorser() throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("teamName", "TEST TEAM");
        params.put("endorser", "WUNE");
        ObjectMapper mapper = new ObjectMapper();
        String requestJson = mapper.writer().withDefaultPrettyPrinter().writeValueAsString(params);

        Team team = teamBuilder.defaultBuilder()
                .withTeamName("TEST TEAM")
                .withTeamDesc("TEST DESC")
                .saveToDB();
        Users user = usersBuilder.defaultBuilder()
                .withStaffId("WUNE")
                .withDomainId("WUNE")
                .saveToDB();
        TeamEndorser teamEndorser = teamEndorserBuilder.defaultBuilder()
                .withEndorser("WUNE")
                .withUser(user)
                .saveToDB();
        team.setTeamEndorsers(Arrays.asList(teamEndorser));
        teamRepository.save(team);

        mockMvc.perform(post("/rest/teamEndorser/addTeamEndorserWithTeamAndEndorser")
                .contentType(MediaType.APPLICATION_JSON)
                .content(requestJson))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.endorser").value(teamEndorser.getEndorser()))
                .andExpect(jsonPath("$.users.staffId").value(teamEndorser.getUsers().getStaffId()))
                .andExpect(jsonPath("$.users.domainId").value(teamEndorser.getUsers().getDomainId()))
                .andReturn().getResponse().getContentAsString();
    }
}